home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tmemmv.c < prev    next >
C/C++ Source or Header  |  1992-11-07  |  3KB  |  115 lines

  1. /* Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  2.    Contributed by Torbjorn Granlund (tege@sics.se).
  3.  
  4. This file is part of the GNU C Library.
  5.  
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10.  
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with the GNU C Library; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include "ansidecl.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #ifdef __hpux
  27. #define random rand
  28. #endif
  29.  
  30. int
  31. DEFUN(main, (argc, argv),
  32.       int argc AND char **argv)
  33. {
  34.   char *mem, *memp;
  35.   char *rand_mem;
  36.   char *lo_around, *hi_around;
  37.   int size, max_size;
  38.   int src_off, dst_off;
  39.   int i;
  40.   int space_around = 10;
  41.  
  42.   max_size = 256;
  43.  
  44.   mem = malloc (max_size + 2 * max_size + 2 * space_around);
  45.   rand_mem = malloc (max_size);
  46.   lo_around = malloc (space_around);
  47.   hi_around = malloc (space_around);
  48.   memp = mem + space_around;
  49.  
  50.   /* Fill RAND_MEM with random bytes, each non-zero.  */
  51.   for (i = 0; i < max_size; i++)
  52.     {
  53.       int x;
  54.       do
  55.     x = random ();
  56.       while (x == 0);
  57.       rand_mem[i] = x;
  58.     }
  59.  
  60.   for (size = 0; size < max_size; size++)
  61.     {
  62.       printf("phase %d\n", size);
  63.       for (src_off = 0; src_off <= 16; src_off++)
  64.     {
  65.       for (dst_off = 0; dst_off <= 16; dst_off++)
  66.         {
  67.           /* Put zero around the intended destination, to check
  68.          that it's not clobbered.  */
  69.           for (i = 1; i < space_around; i++)
  70.         {
  71.           memp[dst_off - i] = 0;
  72.           memp[dst_off + size - 1 + i] = 0;
  73.         }
  74.  
  75.           /* Fill the source area with known contents.  */
  76.           for (i = 0; i < size; i++)
  77.         memp[src_off + i] = rand_mem[i];
  78.  
  79.           /* Remember the contents around the destination area.
  80.          (It might not be what we wrote some lines above, since
  81.          the src area and the dst area overlap.)  */
  82.           for (i = 1; i < space_around; i++)
  83.         {
  84.           lo_around[i] = memp[dst_off - i];
  85.           hi_around[i] = memp[dst_off + size - 1 + i];
  86.         }
  87.  
  88.           memmove (memp + dst_off, memp + src_off, size);
  89.  
  90.           /* Check that the destination area has the same
  91.          contents we wrote to the source area.  */
  92.           for (i = 0; i < size; i++)
  93.         {
  94.           if (memp[dst_off + i] != rand_mem[i])
  95.             abort ();
  96.         }
  97.  
  98.           /* Check that the area around the destination is not
  99.          clobbered.  */
  100.           for (i = 1; i < space_around; i++)
  101.         {
  102.           if (memp[dst_off - i] != lo_around[i])
  103.             abort ();
  104.           if (memp[dst_off + size - 1 + i] != hi_around[i])
  105.             abort ();
  106.         }
  107.         }
  108.     }
  109.     }
  110.  
  111.   puts ("Test succeeded.");
  112.  
  113.   return 0;
  114. }
  115.